<?
error_reporting (E_ALL ^ E_NOTICE);
@session_start();

$upload_dir = "upload_pic"; 				// The directory for the images to be saved in
$upload_path = $upload_dir."/";				// The path to where the image will be saved
$large_image_prefix = "resize_"; 			// The prefix name to large image
$thumb_image_prefix = "thumbnail_";			// The prefix name to the thumb image
$large_image_name = $large_image_prefix.$_SESSION['random_key'];     // New name of the large image (append the timestamp to the filename)
$thumb_image_name = $thumb_image_prefix.$_SESSION['random_key'];     // New name of the thumbnail image (append the timestamp to the filename)
$max_file = "4"; 							// Maximum file size in MB
$max_width = "500";							// Max width allowed for the large image
$thumb_width_small = "198";					// Width of thumbnail image : for SmallDisplay
$thumb_height_small = "149";				// Height of thumbnail image : for SmallDisplay
$thumb_width = ceil($thumb_width_small * 1.78);			// Width of thumbnail image : for FullFrame
$thumb_height = ceil($thumb_height_small * 1.78);		// Height of thumbnail image : for FullFrame
$thumb_display_width = "234";
$thumb_display_height = "175";
// Only one of these image types should be allowed for upload
$allowed_image_types = array('image/pjpeg'=>"jpg",'image/jpeg'=>"jpg",'image/jpg'=>"jpg",'image/png'=>"png",'image/x-png'=>"png",'image/gif'=>"gif");
$allowed_image_ext = array_unique($allowed_image_types); // do not change this
$image_ext = "";	// initialise variable, do not change this.

$targetLocationFullFrame = "profileImage/FullFrame";
$targetLocationSmallDisplay = "profileImage/SmallDisplay";
foreach ($allowed_image_ext as $mime_type => $ext) {
    $image_ext.= strtoupper($ext)." ";
}

//TODO: Set Session RandomKey with real users id
if (!isset($_SESSION['random_key']) || strlen($_SESSION['random_key'])==0){
    $_SESSION['random_key'] = strtotime(date('Y-m-d H:i:s')); //assign the timestamp to the session variable
	$_SESSION['user_file_ext']= "";
}

function CreateImage($imageType,$image)
{
	switch($imageType) {
		case "image/gif":
			$source=imagecreatefromgif($image); 
			break;
	    case "image/pjpeg":
		case "image/jpeg":
		case "image/jpg":
			$source=imagecreatefromjpeg($image); 
			break;
	    case "image/png":
		case "image/x-png":
			$source=imagecreatefrompng($image); 
			break;
  	}
	return $source;
}

function Img($imageType,$newImage,$image)
{
	switch($imageType) {
		case "image/gif":
	  		imagegif($newImage,$image); 
			break;
      	case "image/pjpeg":
		case "image/jpeg":
		case "image/jpg":
	  		imagejpeg($newImage,$image,90); 
			break;
		case "image/png":
		case "image/x-png":
			imagepng($newImage,$image);  
			break;
    }
}

function resizeImage($image,$width,$height,$scale) {
	list($imagewidth, $imageheight, $imageType) = getimagesize($image);
	$imageType = image_type_to_mime_type($imageType);
	$newImageWidth = ceil($width * $scale);
	$newImageHeight = ceil($height * $scale);
	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
	
	$source=CreateImage($imageType,$image);
	
	imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);
	
	Img($imageType,$newImage,$image);
	
	chmod($image, 0777);
	return $image;
}

function resizeThumbnailImage($thumb_image_name, $image, $width, $height, $start_width, $start_height,$newImageWidth,$newImageHeight){
	list($imagewidth, $imageheight, $imageType) = getimagesize($image);
	$imageType = image_type_to_mime_type($imageType);
	$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
	
	$source=CreateImage($imageType,$image);
	
	imagecopyresampled($newImage,$source,0,0,$start_width,$start_height,$newImageWidth,$newImageHeight,$width,$height);
		
	Img($imageType,$newImage,$thumb_image_name);
	
	chmod($thumb_image_name, 0777);
	return $newImage;
}

function getHeight($image) {
	$size = getimagesize($image);
	return $size[1];
}

function getWidth($image) {
	$size = getimagesize($image);
	return $size[0];
}
?>